#include <iostream>
#include "modbus_protocol.h"

int main() {
    // 获取Modbus协议管理器实例
    ModbusProtocolManager& manager = ModbusProtocolManager::getInstance();

    // 初始化所有寄存器
    manager.initializeRegisters();

    // 示例：读取一个只读的UInt16寄存器
    UInt16Register* has_valid_measurement_value = static_cast<UInt16Register*>(manager.getRegister(0));
    if (has_valid_measurement_value) {
        std::cout << "Register Name: " << has_valid_measurement_value->getName() << std::endl;
        // 模拟Modbus数据
        std::vector<uint16_t> modbus_data = {1}; // 假设有有效测量值
        if (has_valid_measurement_value->parseValue(modbus_data)) {
            std::cout << "Value: " << has_valid_measurement_value->getValue() << std::endl;
        }
    }

    // 示例：读取一个只读的Float32寄存器
    Float32Register* measurement_value = static_cast<Float32Register*>(manager.getRegister(1));
    if (measurement_value) {
        std::cout << "Register Name: " << measurement_value->getName() << std::endl;
        // 模拟Modbus数据 (IEEE 754 float 123.45)
        // 123.45 的十六进制表示为 0x42F6E666
        std::vector<uint16_t> modbus_data = {0x42F6, 0xE666};
        if (measurement_value->parseValue(modbus_data)) {
            std::cout << "Value: " << measurement_value->getValue() << std::endl;
        }
    }

    // 示例：写入一个读写的UInt16寄存器
    UInt16Register* calibrate_standard_one = static_cast<UInt16Register*>(manager.getRegister(197));
    if (calibrate_standard_one) {
        std::cout << "Register Name: " << calibrate_standard_one->getName() << std::endl;
        calibrate_standard_one->setValue(1); // 写入1执行
        std::vector<uint16_t> data_to_send = calibrate_standard_one->toModbusData();
        std::cout << "Data to send for calibration: " << data_to_send[0] << std::endl;
    }

    // 示例：获取仪器状态并打印
    UInt16Register* instrument_status_reg = static_cast<UInt16Register*>(manager.getRegister(18));
    if (instrument_status_reg) {
        std::vector<uint16_t> status_data = {static_cast<uint16_t>(InstrumentStatus::FAULT)};
        if (instrument_status_reg->parseValue(status_data)) {
            InstrumentStatus status = static_cast<InstrumentStatus>(instrument_status_reg->getValue());
            std::cout << "Instrument Status: ";
            switch (status) {
                case InstrumentStatus::RUNNING: std::cout << "运行"; break;
                case InstrumentStatus::MAINTENANCE: std::cout << "维护"; break;
                case InstrumentStatus::FAULT: std::cout << "故障"; break;
                case InstrumentStatus::CALIBRATION: std::cout << "校准"; break;
                case InstrumentStatus::CHECK: std::cout << "校验"; break;
                case InstrumentStatus::MEASURING: std::cout << "测量"; break;
                case InstrumentStatus::STANDARD: std::cout << "标定"; break;
                case InstrumentStatus::STANDBY: std::cout << "待机"; break;
                case InstrumentStatus::OPERATION_MAINTENANCE: std::cout << "运维"; break;
                case InstrumentStatus::OTHER: std::cout << "其他"; break;
            }
            std::cout << std::endl;
        }
    }

    return 0;
}


